package com.hunk.time.model;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class FruitCart {

    private List<FruitOrder> fruitOrderList;
    private CartRule cartRule;


    public FruitCart() {
    }

    public FruitCart(CartRule cartRule) {
        this.cartRule = cartRule;
    }

    public FruitCart addFruitOrder(FruitOrder fruitOrder) {
        if (fruitOrder == null) throw new RuntimeException("水果订单不能为空");
        if (fruitOrderList == null) {
            fruitOrderList = new ArrayList<>();
        }

        fruitOrderList.add(fruitOrder);
        return this;
    }

    public BigDecimal calTotalPrice() {
        BigDecimal totalPrice = new BigDecimal(0);
        for (FruitOrder fruitOrder : fruitOrderList) {
            BigDecimal orderPrice = fruitOrder.calOrderPrice();
            totalPrice = totalPrice.add(orderPrice);
        }
        if (this.cartRule == null) {
            return totalPrice;
        }
        BigDecimal cartRuleConditionPrice = this.cartRule.getConditionPrice();
        if (totalPrice.compareTo(cartRuleConditionPrice) < 0) return totalPrice;
        BigDecimal cartRuleDiscount = this.cartRule.getDiscount();

        BigDecimal divide = totalPrice.divide(cartRuleConditionPrice, 0, RoundingMode.FLOOR);
        BigDecimal totalDiscountPrice = cartRuleDiscount.multiply(divide);

        return totalPrice.subtract(totalDiscountPrice);
    }

}
